from httplib import HTTPConnection, HTTPSConnection
import re, time

class UrlTest:
    def __init__(self, url):
        https, host, port = self.parse_url(url)
        if https:
            hconn = HTTPSConnection(host, port)
        else:
            hconn = HTTPConnection(host, port)
        self.httpconn = hconn

    def parse_url(self, url):
        if url.find('https') == 0:
            https = True
        else:
            https = False
        _url = re.sub('https?:*/*', '', url)
        found = re.match('([\w\.-]+):(\d+)', _url)
        if found:
            host = found.group(1)
            port = int(found.group(2))
        else:
            found = re.match('([\w\.-]+)', _url)
            if found:
                host = found.group(1)
                port = https and 443 or 80
            else:
                raise(Exception("InvalidURL"))
        return https, host, port

    def test(self, url='/'):
        hconn = self.httpconn
        t0 = time.time()
        hconn.connect()
        et1 = int(1000*(time.time() - t0))
        peer = hconn.sock.getpeername()
        t0 = time.time()
        hconn.request('GET', url)
        resp = hconn.getresponse()
        content = resp.read()
        et2 = int(1000*(time.time() - t0))
        if len(content) < 500:
            print content
        return peer[0], peer[1], et1, et2, len(content)

    def get(self):
        pass

if __name__ == '__main__':
    url_list = ["http://www.google.com",
                "http://www.bing.com/",
                "http://www.yahoo.com",
                "https://www.google.com/accounts/ServiceLogin?service=mailr",
                "https://login.yahoo.com",
                "http://www.flickr.com",
                "http://www.comcast.net/",
                "http://www.comcast.com",
                "http://slashdot.org"]

    Test = {}
    for url in url_list:
        Test[url] = UrlTest(url)

    for i in range(10):
        for url, test in Test.items():
            svr_ip, svr_port, et1, et2, bytes = test.get(url)
            print "%-30s %-15s:%-4d %4d ms %4d ms %6d bytes" %(url[:30], svr_ip, svr_port, et1, et2, bytes)
